package net.wicp.tams.commons.test;

import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import org.junit.Test;

import net.wicp.tams.commons.apiext.PwdUtil;

public class AesKeyGenerator {

	@Test
	public void testAES() throws NoSuchAlgorithmException {
		//Security.addProvider(new BouncyCastleProvider());
	    //实例化支持AES算法的密钥生成器(算法名称命名需按规定，否则抛出异常)  
	    KeyGenerator keygen = KeyGenerator.getInstance("AES");
	    keygen.init(128);
	    //生成密钥  
	    SecretKey deskey = keygen.generateKey(); 
	    
	    // 输出密匙
	    System.out.println("key:");
	    String key = "lWFbmab958Rcn7/ciXogOQ==";// Base64.encodeBase64String(deskey.getEncoded());
	    System.out.println(key);
	    
	    String data = "{\"agent_code\":\"01234567891\"}";
	    String encrypt[] = AesCryptoUtil.encrypt(data, key);
	    System.out.println("iv:"+encrypt[0]+",encrypt:"+encrypt[1]);
	    String decrypt = AesCryptoUtil.decrypt(encrypt[1],key,encrypt[0]);
	    System.out.println(decrypt);
	}
	
	@Test
	public void testAES2() throws NoSuchAlgorithmException {
		for (int i = 0; i < 20; i++) {
			String tempstr="{\"agent_code\":\"0123456789"+i+"\"}";
			String[] pwd= PwdUtil.encryptByAes( "{\"agent_code\":\"0123456789"+i+"\"}");
			System.out.println("oristr="+tempstr+"   encrypt="+pwd[0]+"  iv="+pwd[1]);
		}
		
		
		//String ori=PwdUtil.decryptByAes(pwd[0], pwd[1]);
	}
}

